package org.hsc.httpProxy;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpProxyServer {
	private static Logger logger = LoggerFactory.getLogger(HttpProxyServer.class); 
	
	public static void main(String[] args) throws IOException {

		Thread th=new Thread(new Runnable() {
			@Override public void run() {
					try {
						HttpProxyServer hs=new HttpProxyServer();
						hs.start();
					} catch (IOException e) {
						logger.error(e.getMessage(),e);
					}
				}
			});
		th.setDaemon(false);
		th.start();
	}
	
	public void start() throws IOException{
				
		IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new HttpProtocolEncoder(),new HttpProtocolDecoder()));
        acceptor.setHandler(  new ServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
		acceptor.bind( new InetSocketAddress(8083) );
				
	}

}
